package com.wlk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wlk.mapper.UserMapper;
import com.wlk.mapper.UserRoleMapper;
import com.wlk.pojo.User;
import com.wlk.pojo.dto.AddUserDTO;
import com.wlk.pojo.dto.UserDTO;
import com.wlk.service.UserService;
import com.wlk.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserRoleMapper userRoleMapper;

    /**
     * @description: login登录业务
     * @author:
     * @date: 2024/6/12 下午2:40
     * @param:
     * @return:
     **/
    @Override
    public Result login(String username, String password) {
        //1.先验证用户信息是否正确
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", username).eq("password",password);
        User user = userMapper.selectOne(userQueryWrapper);
        //2.登录验证失败
        if(user==null){
            return Result.fail("用户名或密码错误");
        }
        //3.验证成功，返回用户的UserDTO对象（内含身份列表）
        UserDTO userDTO = userMapper.getUserWithRolesByUsernameAndPassword(username, password);
        return Result.ok(userDTO);

    }

    /**
     * @description: 获取用户列表
     * @author:
     * @date: 2024/6/12 下午3:38
     * @param:
     * @return:
     **/
    @Override
    public Result getUserList() {
        List<User> userList = userMapper.selectList(null);
        if (userList != null && userList.size() > 0){
            return Result.ok(userList);
        }else {
            return Result.ok("没有用户数据");
        }
    }

    @Override
    @Transactional
    public Result addUser(AddUserDTO addUserDTO) {
        // 验证用户名在数据库中不存在
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", addUserDTO.getUsername());
        User user = userMapper.selectOne(userQueryWrapper);
        if (user!= null) {
            return Result.fail("用户名已存在");
        }
        // 验证邮箱在数据库中不存在
        QueryWrapper<User> emailQueryWrapper = new QueryWrapper<>();
        emailQueryWrapper.eq("email", addUserDTO.getEmail());
        User emailUser = userMapper.selectOne(emailQueryWrapper);
        if (emailUser!= null) {
            return Result.fail("邮箱已存在");
        }
        // 验证手机号在数据库中不存在
        QueryWrapper<User> phoneQueryWrapper = new QueryWrapper<>();
        phoneQueryWrapper.eq("phone", addUserDTO.getPhone());
        User phoneUser = userMapper.selectOne(phoneQueryWrapper);
        if (phoneUser!= null) {
            return Result.fail("手机号已存在");
        }

        // 保存用户信息
        User user1 = new User();
        user1.setUsername(addUserDTO.getUsername());
        user1.setPassword(addUserDTO.getPassword());
        user1.setEmail(addUserDTO.getEmail());
        user1.setPhone(addUserDTO.getPhone());
        user1.setNickname(addUserDTO.getNickname());
        Integer i = userMapper.insert(user1);

        if (i > 0) {
            Integer userId = userMapper.getUserIdByUsername(addUserDTO.getUsername());
            System.out.println(userId+"=======================================");
            // 保存用户角色关系
            userRoleMapper.insertUserRole(userId, addUserDTO.getRoleId());
            return Result.ok("添加用户成功");
        } else {
            return Result.fail("添加用户失败");
        }

    }
    /**
     * @description: 为添加部门提供部门负责人数据，获取所有的用户的id和nickname
     * @author:
     * @date: 2024/6/16 下午11:42
     * @param:
     * @return:
     **/
    @Override
    public Result toAddDep() {
        return Result.ok(userMapper.getListToAddDep());
    }

    @Override
    public Result getListToGrant(String proCode) {
        return Result.ok(userMapper.getListToGrant(proCode));
    }
}
